package views;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

import controllers.TurretTrackingSystem;

public class TurretControlpanel extends JFrame implements ActionListener{
	
	public static final int size_x = 220;
	public static final int size_y = 220;
	
	public static final int screen_x_pos = 100;
	public static final int screen_y_pos = 100;
	
	private static JButton toggleTurretButton;
	private JLabel onOffLabelDescriptionLabel = new JLabel("Turret status:");
	private JLabel onOffLabel;
	private JLabel statusLabelDescriptionLabel = new JLabel("Turret action:");
	private JLabel statusLabel;
	
	private JLabel coolLabel1 = new JLabel("Copyright (c) kakben and leven");
	private JLabel coolLabel2 = new JLabel("dynalogic annihilation systems,");
	private JLabel coolLabel3 = new JLabel("all rights including kills reserved.");
	
	public TurretControlpanel() {
		
		this.setSize(size_x,size_y);
		this.setLocation(screen_x_pos, screen_y_pos);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setTitle("TTS " + TurretTrackingSystem.version);
		this.setLayout(null);
		
		toggleTurretButton = new JButton("Toggle turret on/off");
		toggleTurretButton.setBounds(25,25,150,30);
		toggleTurretButton.addActionListener(this);
		this.add(toggleTurretButton);
		
		onOffLabelDescriptionLabel.setBounds(25, 70, 100, 20);
		this.add(onOffLabelDescriptionLabel);
		onOffLabel = new JLabel();
		setTurretOnOffLabel();
		onOffLabel.setBounds(130, 70, 210, 20);
		this.add(onOffLabel);
		
		statusLabelDescriptionLabel.setBounds(25, 90, 100, 20);
		this.add(statusLabelDescriptionLabel);
		statusLabel = new JLabel("-");
		statusLabel.setBounds(130, 90, 210, 20);
		this.add(statusLabel);
		
		coolLabel1.setBounds(25, 120, 180, 15);
		coolLabel1.setFont(new Font("Verdana",Font.BOLD,9));
		this.add(coolLabel1);
		coolLabel2.setBounds(25, 135, 180, 15);
		coolLabel2.setFont(new Font("Verdana",Font.BOLD,9));
		this.add(coolLabel2);
		coolLabel3.setBounds(25, 150, 180, 15);
		coolLabel3.setFont(new Font("Verdana",Font.BOLD,9));
		this.add(coolLabel3);
		
		this.setResizable(false);
		this.setVisible(true);
		
	}
	
	/**
	 * Turns turret online/offline
	 * @throws InterruptedException 
	 */
	private void toggleTurret() throws InterruptedException {
		TurretTrackingSystem.toggleTurret();
		if(TurretTrackingSystem.turretOnline) {
			statusLabel.setText("Idle");
		}
		else {
			statusLabel.setText("-");
		}
		setTurretOnOffLabel();
	}
	
	/**
	 * Method for external components to change the status label of the main interface.
	 * @return Returns a boolean - true if the label was changed successfully or false if the turret was offline.
	 */
	public boolean setStatusLabel(String text) {
		if(TurretTrackingSystem.turretOnline) {
			statusLabel.setText(text);
			return true;
		}
		else
			return false;
	}
	
	/**
	 * Sets the on/off label for the turret safely, given the static variable in the main class.
	 */
	private void setTurretOnOffLabel() {
		if(TurretTrackingSystem.turretOnline){
			onOffLabel.setText("Online");
			onOffLabel.setForeground(Color.GREEN);
		}
		else {
			onOffLabel.setText("Offline");
			onOffLabel.setForeground(Color.RED);
		}
			
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == toggleTurretButton){
				try {
					toggleTurret();
				} catch (InterruptedException e1) {

				}
		}
	}
	
	public void disableToggleButton() {
		toggleTurretButton.setEnabled(false);
	}
	public void enableToggleButton() {
		toggleTurretButton.setEnabled(true);
	}
	
}
